package cn.yunhe.dao.impl;

import cn.yunhe.dao.ITopicDao;
import cn.yunhe.entity.Admin;
import cn.yunhe.entity.Topic;
import lombok.Getter;
import lombok.Setter;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.simple.ParameterizedRowMapper;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;


@Component
public class TopicDao implements ITopicDao {

   /* public static final RowMapper rowMapper = new ParameterizedRowMapper() {
        @Override
        public Object mapRow(ResultSet rs, int i) throws SQLException {
            Topic topic = new Topic();
            topic.setTid(rs.getInt("tid"));
            topic.setTitle(rs.getString("title"));
            topic.setContext(rs.getString("context"));
            topic.setPic(rs.getString("pic"));
            Admin admin = new Admin();
            admin.setA_id(rs.getInt("a_id"));
            admin.setA_name(rs.getString("a_name"));
            admin.setA_pwd(rs.getString("a_pwd"));
            topic.setAdmin(admin);
            return topic;
        }
    };*/

/*    @Override
    public List<Topic> searchByKey(Topic topic) {
        StringBuffer sql = new StringBuffer("select t.tid,t.title,t.context,t.pic,a.a_id,a.a_name,a_pwd from tb_topic t inner join tb_admin a on t.aid=a.a_id where 1=1 ");
        List<String> list = new ArrayList<>();
        if (topic != null) {
            if (topic.getTitle() != null && topic.getTitle().trim().length() > 0) {
                sql.append("and t.title=?");
                list.add("%" + topic.getTitle() + "%");
            }
            if (topic.getContext() != null && topic.getContext().trim().length() > 0) {
                sql.append("and t.context=?");
                list.add("%" + topic.getContext() + "%");
            }
            if (topic.getAdmin().getA_name() != null && topic.getAdmin().getA_name().trim().length() > 0) {
                sql.append("and a.a_name=?");
                list.add("%" + topic.getAdmin().getA_name() + "%");
            }
        }
        Object[] params = list.toArray();
        return getJdbcTemplate().query(sql.toString(), params, rowMapper);
    }



    @Override
    public List<Topic> searchAll() {
        String sql = "select t.tid,t.title,t.context,t.pic,a.a_id,a.a_name,a.a_pwd from tb_topic t inner join tb_admin a on t.aid=a.a_id";
        List<Topic> list = getJdbcTemplate().query(sql, rowMapper);
        return list;
    }

    @Override
    public int save(Topic topic) {
        String sql = "insert into tb_topic(title,context,pic) values(?,?,?)";
        Object[] params = {topic.getTitle(), topic.getContext(), topic.getPic()};
        return getJdbcTemplate().update(sql, params);
    }

    @Override
    public int del(int tid) {
        String sql = "delete from tb_topic where tid=?";
        Object[] params = {tid};
        return getJdbcTemplate().update(sql, params);
    }

    @Override
    public int up(Topic topic) {
        String sql = "update tb_topic set title=? where tid=?";
        Object[] params = {topic.getTitle(), topic.getTid()};
        return getJdbcTemplate().update(sql, params);
    }*/


    @Resource
    private JdbcTemplate jdbcTemplate;

    public static final RowMapper rowMapper = new ParameterizedRowMapper() {
        @Override
        public Object mapRow(ResultSet rs, int i) throws SQLException {
            Topic topic = new Topic();
            topic.setTid(rs.getInt("tid"));
            topic.setTitle(rs.getString("title"));
            Admin admin = new Admin();
            admin.setA_id(rs.getInt("a_id"));
            admin.setA_name(rs.getString("a_name"));
            topic.setAdmin(admin);
            return topic;
        }
    };


    @Override
    public List<Topic> searchByKey(Topic topic) {
        StringBuffer sql = new StringBuffer("select t.tid,t.title,a.a_id,a.a_name from tb_topic t inner join tb_admin a on t.aid=a.a_id where 1=1 ");
        List<String> list = new ArrayList<>();
        if (topic != null) {
            if (topic.getTitle() != null && topic.getTitle().trim().length() != 0) {
                sql.append("and t.title like ?");
                list.add("%" + topic.getTitle() + "%");
            }
            if (topic.getAdmin() != null && topic.getAdmin().getA_name().trim().length() > 0) {
                sql.append("and a.a_name like ?");
                list.add(topic.getAdmin().getA_name());
            }
        }
        Object[] params = list.toArray();
        return jdbcTemplate.query(sql.toString(), params, rowMapper);
    }

    @Override
    public List<Topic> searchAll() {
        String sql = "select t.tid,t.title,a.a_id,a.a_name from tb_topic t inner join tb_admin a on t.aid=a.a_id";
        List<Topic> list = jdbcTemplate.query(sql, rowMapper);
        return list;
    }

    @Override
    public int save(Topic topic) {
        String sql = "insert into tb_topic(title) values(?)";
        Object[] params = {topic.getTitle()};
        return jdbcTemplate.update(sql, params);
    }

    @Override
    public int del(int tid) {
        String sql = "delete from tb_topic where tid=?";
        Object[] params = {tid};
        return jdbcTemplate.update(sql, params);
    }

    @Override
    public int up(Topic topic) {
        String sql = "update tb_topic set title=? where tid=?";
        Object[] params = {topic.getTitle(), topic.getTid()};
        return jdbcTemplate.update(sql, params);
    }

}
